package com.Cychat.doctor.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Doctor;
import com.Cychat.commons.entity.pojo.TreatmentRecord;
import com.Cychat.commons.entity.vo.DoctorVo;
import com.Cychat.commons.entity.vo.TreatmentVo;
import com.Cychat.doctor.service.DoctorService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auther: 谢宝辉  <br/>
 * Date: 2025/11/3:17:18  <br/>
 * Description: 医生Controller
 */
@RestController
@RequestMapping("/api/doctor")
//@CrossOrigin
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    /**
     * Description : 获取所有医生信息，并联查出其科室信息和所在的医院信息
     * ChangeLog : 1. 创建 (2025/11/3 17:19 [Lenovo]);
     *
     * @param
     * @return null
     * @auther: 谢宝辉
     * date: 2025/11/3:17:19
     */
    @GetMapping("/getAllDoctors")
    public ResultMessage<List<DoctorVo>> getAllDoctors() {
        List<DoctorVo> doctorList = doctorService.getAllDoctors();
        if (doctorList.isEmpty()) {
            return ResultMessage.error("当前没有医生信息");
        }
        return ResultMessage.success(doctorList);
    }

    /**
     * 前端getDoctorList：分页+筛选获取医生列表
     */
    @GetMapping("/list")
    public ResultMessage<IPage<DoctorVo>> getDoctorList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "hospitalId", required = false) Integer hospitalId,
            @RequestParam(value = "departmentId", required = false) Integer departmentId) {
        Page<Doctor> page = new Page<>(pageNum, pageSize);
        IPage<DoctorVo> doctorPage = doctorService.getDoctorVoPage(page, hospitalId, departmentId);
        return ResultMessage.success(doctorPage);
    }

    /**
     * 前端addDoctor：新增医生
     */
    @PostMapping("/add")
    public ResultMessage<Boolean> addDoctor(@RequestBody Doctor doctor) {
        if (doctorService.checkUserInfoIdExists(doctor.getUserinfoId())) {
            return ResultMessage.error("该用户已关联医生信息，无法重复添加");
        }
        boolean result = doctorService.save(doctor);
        return result ? ResultMessage.success(result) : ResultMessage.error("新增医生信息失败");
    }

    /**
     * 前端editDoctor：编辑医生
     */
    @PostMapping("/edit")
    public ResultMessage<Boolean> editDoctor(@RequestBody Doctor doctor) {
        if (doctorService.getById(doctor.getDoctorId()) == null) {
            return ResultMessage.error("医生信息不存在");
        }
        boolean result = doctorService.updateDoctor(doctor);
        return result ? ResultMessage.success(result) : ResultMessage.error("编辑医生信息失败");
    }

    /**
     * 原有接口：删除医生
     */
    @DeleteMapping("/deleteDoctor/{doctorId}")
    public ResultMessage<Boolean> deleteDoctor(@PathVariable("doctorId") Integer doctorId) {
        boolean result = doctorService.removeById(doctorId);
        return result ? ResultMessage.success(result) : ResultMessage.error("删除医生信息失败");
    }

    /**
     * 前端getDoctorInfo：获取医生详情
     */
    @GetMapping("/info/{doctorId}")
    public ResultMessage<DoctorVo> getDoctorInfo(@PathVariable("doctorId") Integer doctorId) {
        DoctorVo doctorVo = doctorService.getDoctorVoById(doctorId);
        return doctorVo != null ? ResultMessage.success(doctorVo) : ResultMessage.error("医生信息不存在");
    }

    /**
     * 前端updateDoctorInfo：更新医生个人信息
     */
    @PostMapping("/update")
    public ResultMessage<Boolean> updateDoctorInfo(@RequestBody Doctor doctor) {
        return editDoctor(doctor);
    }

    /**
     * 根据userinfo_id获取当前登录医生信息
     */
    @GetMapping("/current/{userinfoId}")
    public ResultMessage<DoctorVo> getCurrentDoctor(@PathVariable("userinfoId") Integer userinfoId) {
        DoctorVo doctorVo = doctorService.getDoctorVoByUserInfoId(userinfoId);
        return doctorVo != null ? ResultMessage.success(doctorVo) : ResultMessage.error("当前用户未关联医生信息");
    }
}
